package tv.dyndns.kishibe.server;

import java.util.Date;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import tv.dyndns.kishibe.client.Constant;
import tv.dyndns.kishibe.client.packet.PacketUserData;

public class ThemeModeEditorManager {
	public boolean isThemeModeEditor(int userCode) {
		final Map<Integer, Integer> data = CachedDatabase.getInstance().getThemeModeEditorsStatus();
		return data.containsKey(userCode) && data.get(userCode) == Constant.THEME_MODE_EDITOR_STATUS_ACCEPTED;
	}

	public void applyThemeModeEditor(int userCode, String text) {
		CachedDatabase.getInstance().updateThemeModeEdtorsStatus(userCode, Constant.THEME_MODE_EDITOR_STATUS_APPLYING);

		try {
			// メール送信
			final PacketUserData userData = CachedDatabase.getInstance().getUserData(userCode);
			final Properties properties = System.getProperties();
			properties.put("mail.smtp.host", "smtp03.odn.ne.jp");
			final Session session = Session.getDefaultInstance(properties, null);
			MimeMessage mimeMessage = new MimeMessage(session);
			mimeMessage.setFrom(new InternetAddress("hal25870@rio.odn.ne.jp", "QMAClone.ThemeModeEditorManager", "iso-2022-jp"));
			mimeMessage.setRecipients(Message.RecipientType.TO, "hal25870@rio.odn.ne.jp");
			mimeMessage.setSubject("[QMAClone]テーマモード編集権限申請 " + userData.playerName, "iso-2022-jp");

			final StringBuilder sb = new StringBuilder();
			sb.append("QMAClone管理者様:\n\n");
			sb.append("テーマモード編集権限の申請がありました。\n");
			sb.append("プレイヤーコード : ").append(userData.userCode).append("\n");
			sb.append("プレイヤー名 : ").append(userData.playerName).append("\n");
			sb.append("プレイ回数 : ").append(userData.playCount).append("\n");
			sb.append("レーティング : ").append(userData.rating).append("\n");
			sb.append("一言 : ").append(text).append("\n");
			sb.append("以上、よろしくお願いいたします。\n\n");
			sb.append("http://kishibe.dyndns.tv:8080/QMAClone/#administratormode\n");

			mimeMessage.setText(sb.toString(), "iso-2022-jp");
			mimeMessage.setHeader("Content-Type", "text/plain");
			mimeMessage.setSentDate(new Date());
			Transport.send(mimeMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void acceptThemeModeEditor(int userCode) {
		CachedDatabase.getInstance().updateThemeModeEdtorsStatus(userCode, Constant.THEME_MODE_EDITOR_STATUS_ACCEPTED);
	}

	public void rejectThemeModeEditor(int userCode) {
		CachedDatabase.getInstance().updateThemeModeEdtorsStatus(userCode, Constant.THEME_MODE_EDITOR_STATUS_REJECTED);
	}

	public Map<Integer, Integer> getThemeModeEditors() {
		return CachedDatabase.getInstance().getThemeModeEditorsStatus();
	}

	public boolean isApplyingThemeModeEditor(int userCode) {
		final Map<Integer, Integer> data = CachedDatabase.getInstance().getThemeModeEditorsStatus();
		return data.containsKey(userCode) && data.get(userCode) == Constant.THEME_MODE_EDITOR_STATUS_APPLYING;
	}
}
